package com.android.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.R;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.model.login.LoginListener;
import com.android.model.login.LoginModel;
import com.android.model.login.LoginModelImpl;
import com.android.utils.watcher.MyWatcher;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class LoginActivity extends AppCompatActivity implements LoginListener, View.OnClickListener {
    private static final String TAG ="LoginActivity";

    private ResponseData<User> responseData;
    private EditText etPwd;
    private EditText etAccount;
    private Button LoginButton;
    private Button getVerificationCode;
    private Button RegisterButton;

    private String account;
    private String password;
    private LoginModel loginModel = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginModel = new LoginModelImpl();

        etPwd = findViewById(R.id.et_pwd);
        etAccount = findViewById(R.id.et_acc);
        getVerificationCode = findViewById(R.id.bt_getCode);
        LoginButton = findViewById(R.id.bt_login);
        RegisterButton = findViewById(R.id.bt_register);


        //监视手机号格式 注册监听在里面
        etAccount.addTextChangedListener(new MyWatcher(getVerificationCode,RegisterButton,this));
        //获取验证码按钮
        getVerificationCode.setOnClickListener(this);
        //登录按钮
        LoginButton.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        account = etAccount.getText().toString();
        switch (v.getId()){
            case R.id.bt_getCode:
                loginModel.sendVerifyCode(account,this);
                Toast.makeText(LoginActivity.this, "验证码发送成功！",
                        Toast.LENGTH_SHORT).show();
                Log.e("TAG","验证码");break;
            case R.id.bt_login:Log.e("TAG","登录");
                password = etPwd.getText().toString();
                loginModel.longin(account,password,this);
            break;
            case R.id.bt_register:
                password = etPwd.getText().toString();
                loginModel.registerUser(account,password,this);
                break;
        }
    }
    private void login() {
        if(responseData.getCode() == 200){
            Intent intent = new Intent(LoginActivity.this,HomePageActivity.class);
            //发送登录的用户信息
            EventBus.getDefault().postSticky(responseData);
            Log.e("userId",responseData.getData().getId().toString());

            startActivity(intent);
        }else {
            Toast.makeText(LoginActivity.this, "登录信息有误！请仔细检查！",
                    Toast.LENGTH_SHORT).show();
            Log.e("login",responseData.getMsg());
        }
    }

    @Override
    public void onLoginSuccess(ResponseData<User> responseData) {

        this.responseData = responseData;
        login();


    }


    @Override
    public void onGetCodeSuccess(ResponseData<String> responseData) {
    }

    @Override
    public void onRejisterSuccess(ResponseData<String> responseData) {
        Log.e(TAG,"onRejisterSuccess"+responseData.getCode());
    }


    @Override
    public void onError() {

    }





}